package core;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class View {
	static public String path(String tplName){
		return "./view/" + Config.template + "/" + tplName + ".tpl.jsp";
	}
	
	static public void show(HttpServletRequest request, HttpServletResponse response, String tplName){
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		try{
			RequestDispatcher rd = request.getRequestDispatcher(path(tplName));
			rd.forward(request, response);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	static public void showMessage(HttpServletRequest request, HttpServletResponse response, String message, String redirect_url){
		String tpl_name = "show_message";
		
		if(request.getParameter("ajax") == null){
			if(redirect_url != null){
				if(redirect_url.equals("back")){
					redirect_url = "javascript:history.back()";
				}else if(redirect_url.equals("refresh")){
					redirect_url = request.getRequestURI() + (request.getQueryString() != null ? ("?" + request.getQueryString()) : "");
				}else if(redirect_url.equals("confirm")){
					request.setAttribute("request_url", request.getRequestURI() + "?" + request.getQueryString());
				}
			}
			
		}else{
			if(redirect_url != null){
				if(redirect_url.equals("refresh")){
					redirect_url = request.getRequestURI() + (request.getQueryString() != null ? ("?" + request.getQueryString()) : "");
				}else if(redirect_url.equals("confirm")){
					request.setAttribute("request_url", request.getRequestURI() + "?" + request.getQueryString());
				}
			}
			tpl_name = "show_message_ajax";
		}
		
		request.setAttribute("message", message);
		request.setAttribute("redirect_url", redirect_url);

		show(request, response, tpl_name);
	}
	
	static public void showMessage(HttpServletRequest request, HttpServletResponse response, String message){
		showMessage(request, response, message, null);
	}
	
	static public void alertSystemCrash(HttpServletRequest request, HttpServletResponse response){
		showMessage(request, response, "System crashed. Administrators will soon solve the problem.", null);
	}

	static public String mpage(int total, int page, int limit, String url){
		url += url.indexOf('?') < 0 ? '?' : '&';

		int maxpage = total / limit + 1;
		if(maxpage <= 1){
			return "";
		}
		
		page = Math.min(maxpage, Math.max(1, page));
		String html = "<div class=\"mpage\">";
		html+= "<a href=\"" + url + "page=1\">" + "首页</a>";

		html+= "<a href=\"" + url + "page=" + Math.max(1, page - 1) + "\">上一页</a>";
		int left_max = Math.min(maxpage, page + 5 + Math.max(0, 6 - page));
		for(int i = Math.max(1, page - 5 - Math.max(0,page + 5 - maxpage)); i <= left_max; i++){
			if(i == page){
				html+= "<a href=\"#\" class=\"current\">" + i + "</a>";
			}else{
				html+= "<a href=\"" + url + "page=" + i +"\">" + i + "</a>";
			}
		}
		html+= "<a href=\"" + url + "page=" + Math.min(maxpage, page + 1) + "\">下一页</a>";
		html+= "<a href=\"" + url + "page=" + maxpage + "\">尾页</a>";
		html+= "</div>";
		
		return html;
	}
	
	public static String select(String varname, String[] option_keys, String[] option_values, String value){
		StringBuffer html = new StringBuffer();
		
		html.append("<select id=\"").append(varname).append("\" name=\"").append(varname).append("\">");
		for(int i = 0; i < option_keys.length; i++){
			html.append("<option value=\"").append(option_keys[i]).append("\"");
			if(option_keys[i].equals(value)){
				html.append(" selected=\"selected\" ");
			}
			html.append(">");
			html.append(option_values[i]);
			html.append("</option>");
		}
		html.append("</select>");
		
		return new String(html);
	}
	
	public static String th(int number){
		switch(number % 10){
		case 1:
			return number + "st";
		case 2:
			return number + "nd";
		case 3:
			return number + "rd";
		default:
			return number + "th";
		}
	}
}
